﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperKit.Executor {
  public class RunPython : CommandExecutor {

    private string python_script;
    private string python_args;

    public RunPython(ArgReader arg, IExecutorOwner form, string fpath) : base(arg, form, fpath) {
      python_script = arg.GetParameter("-script");
      python_args = arg.GetParameter("-args");
    }


    public override void Execute() {
      base.Execute();

      DirectoryInfo info = new DirectoryInfo(FilePath);

      // 1. 配置Python解释器和脚本路径
      string pythonInterpreter = "python"; // 或完整路径如 @"C:\Python39\python.exe"

      // 2. 创建进程启动配置
      ProcessStartInfo startInfo = new ProcessStartInfo {
        FileName = pythonInterpreter,
        Arguments = $"\"{python_script}\" \"{FilePath}\" {python_args}", // 用引号包裹路径和参数以防空格
        UseShellExecute = true,  // 关键修改：使用系统Shell
        CreateNoWindow = false   // 关键修改：显示控制台窗口
      };

      // 3. 启动进程并处理结果
      try {
        Flush($"python {startInfo.Arguments}");
        Process.Start(startInfo); // 直接启动进程（不等待结束）
        Console.WriteLine("已启动Python进程");
      } catch (Exception ex) {
        Console.WriteLine($"错误: {ex.Message}");
      }

      End();
    }
  }
}
